﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Gallery : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public string Command { get; set; }
        public string CommandPreview { get; set; }
        public string CommandRevert { get; set; }
        public Enumerations.ElementDimensions ElementDimensions { get; set; }
        public string Id { get; set; }
        public string QueryCommand { get; set; }
        public int? Sequence { get; set; }
        public string Width { get; set; }
        public List<GalleryButton> Buttons { get; set; }

        public Gallery()
        {
            GalleryButton gb = new GalleryButton();
            this.Buttons = new List<GalleryButton>();
            this.Buttons.Add(gb);
        }

        public Gallery(XmlNode node)
        {
            try
            {
                this.Command = XmlHelper.GetAttribute(node, "Command");
                this.CommandPreview = XmlHelper.GetAttribute(node, "CommandPreview");
                this.CommandRevert = XmlHelper.GetAttribute(node, "CommandRevert");
                this.ElementDimensions = (Enumerations.ElementDimensions)Enum.Parse(typeof(Enumerations.ElementDimensions), XmlHelper.GetAttribute(node, "ElementDimensions"));
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.QueryCommand = XmlHelper.GetAttribute(node, "QueryCommand");
                this.Sequence = XmlHelper.GetNullableIntAttribute(node, "Sequence");
                this.Width = XmlHelper.GetAttribute(node, "Width");

                this.Buttons = new List<GalleryButton>();

                foreach (XmlNode gbNode in node.SelectNodes("GalleryButton"))
                {
                    if (gbNode.NodeType == XmlNodeType.Comment)
                        return;

                    GalleryButton gb = new GalleryButton(gbNode);
                    this.Buttons.Add(gb);
                }

                if (this.Buttons.Count == 0)
                {
                    GalleryButton gb = new GalleryButton();
                    this.Buttons.Add(gb);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode galleryNode = XmlHelper.AddNode(commandUINode, "Gallery");
                XmlHelper.AddAttribute(galleryNode, "Command", this.Command);
                XmlHelper.AddAttribute(galleryNode, "CommandPreview", this.CommandPreview);
                XmlHelper.AddAttribute(galleryNode, "CommandRevert", this.CommandRevert);
                XmlHelper.AddAttribute(galleryNode, "ElementDimensions", this.ElementDimensions);
                XmlHelper.AddAttribute(galleryNode, "Id", this.Id);
                XmlHelper.AddAttribute(galleryNode, "QueryCommand", this.QueryCommand);
                XmlHelper.AddAttribute(galleryNode, "Sequence", this.Sequence);
                XmlHelper.AddAttribute(galleryNode, "Width", this.Width);

                foreach (GalleryButton gb in this.Buttons)
                {
                    gb.AddNode(galleryNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Gallery");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "GalleryButton" }, true);

                foreach (GalleryButton gb in this.Buttons)
                {
                    gb.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is GalleryButton))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (this.Buttons == null)
                    {
                        this.Buttons = new List<GalleryButton>();
                    }

                    this.Buttons.Add((GalleryButton)child);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is GalleryButton)
                {
                    this.Buttons.Remove((GalleryButton)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
